package cn.ly.checkin.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ly.checkin.domain.User;

@Repository
@Transactional
public class UserService {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public void createUser(User user){
		sessionFactory.getCurrentSession().save(user);
	}
	@Transactional(readOnly = true)
	public User loadUser(Long user_id){
	Object user=this.sessionFactory.getCurrentSession().load(User.class, user_id);
	return ((User)user);
	}
	
	@Transactional(readOnly = true)
	public User findByNameAndPssword(String name,String pssword){
		String sql="from User where name=:name and pssword=:pssword";
		Query query=this.sessionFactory.getCurrentSession().createQuery(sql);
		query.setString("name",name);
		query.setString("pssword", pssword);
		List<User> users=query.list();
		if(users.size()>0){
			return users.get(0);
		}else{
			return null;
		}
		
	}
}
